package com.start_eternity.oasissafewalk.exception;
import com.start_eternity.oasissafewalk.comment.BaseResponse;
import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * author:ZhangT
 * Date:2024 / 04 / 15 / 14:51
 * Description:     全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    @ExceptionHandler(BusinessException.class)
    public Result<String> getBusinessException(BusinessException e) {
        log.info("BusinessException:" +e.getDescription(),e);
        return ResultUtils.error(e.getCode(),e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<RuntimeException> getRuntimeException(RuntimeException e) {
        log.info("RuntimeException:" +e.getMessage(),e);
        return new BaseResponse<>(ErrorCode.PARAM_ERROR);
    }
}
